<?php

namespace app\api1\controller;

use app\BaseController;
use app\common\model\NewOrderLog;

class PayCommon extends BaseController
{
    public static function addRobot($order_id)
    {
        $newOrder = \app\common\model\NewOrders::find($order_id);
        $data = $newOrder->product_parameters;
        $product_id = $data['id'];
        unset($data['id']);
        unset($data['create_time']);
        unset($data['update_time']);
        $data['member_id'] = $newOrder->member_id;
        $data['name'] = '机器人' . $newOrder->id;
        $data['brief_introduction'] = '机器人' . $newOrder->id . '简介';
        $data['daily_price'] = $newOrder->daily_price;
        $data['expiration_time'] = time() + $data['duration_days'] * 24 * 3600;
        $data['expiration_time'] = date('Y-m-d H:i:s', $data['expiration_time']);
        $data['robot_products_id'] = $product_id;
        $data['is_gpt_answer'] = 1;
        $data['code'] = md5(time() . rand(1000, 9999) .  $product_id );

        $robot = \app\common\model\Robot::create($data);
        return  $robot->id;
    }

    public static function renewRobot($order_id)
    {
        $newOrder = \app\common\model\NewOrders::find($order_id);
        $robot = \app\common\model\Robot::find($newOrder->product_id);
        $robot->expiration_time = date('Y-m-d H:i:s', $robot->getData('expiration_time') + $newOrder->purchase_days * 24 * 3600);
        $robot->save();
    }

    public static function upgradeRobot($order_id)
    {
        $newOrder = \app\common\model\NewOrders::find($order_id);
        $data = $newOrder->product_parameters;
        $product_id = $data['id'];

        $update['daily_price'] = $data['price'] / $data['duration_days'];
        $update['robot_products_id'] = $product_id;
        $update['expiration_time'] =  $data['expire_time'];
        $update['word_limit'] = $data['word_limit'];
        $update['daily_conversation_limit'] = $data['daily_conversation_limit'];
        $update['is_open_api'] = $data['is_open_api'];
        $update['is_remove_brand'] = $data['is_remove_brand'];


        $robot = \app\common\model\Robot::where('id',$newOrder->product_id)->update($update);
    }


    public static function completeRobotOrder($order_id, $pay_method = '系统管理员', $pay_details = [])
    {
        try{
            $newOrder = \app\common\model\NewOrders::find($order_id);
            $newOrder->is_payment = 1;
            $newOrder->pay_time = time();
            $newOrder->payment_method = $pay_method;
            $newOrder->payment_details = $pay_details;

            if($newOrder->purchase_type == 'new'){
                $newOrder->product_id = self::addRobot($order_id);
            }

            if($newOrder->purchase_type == 'renew'){
                self::renewRobot($order_id);
            }

            if($newOrder->purchase_type == 'upgrade'){
                self::upgradeRobot($order_id);
            }

            $newOrder->save();

            NewOrderLog::create(['new_order_id' => $order_id, 'name' => '支付成功']);

            return true;
        }catch (\Exception $e){
            return $e->getMessage();
        }

    }

}